package com.gxa.controller.admin;

import com.gxa.common.R.ResponseData;
import com.gxa.service.admin.AdminService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "系统管理",tags = "系统管理接口API")
@Controller
@RequestMapping("/admin")
@RequiresPermissions({"系统管理"})
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/select_company",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入系统设置",notes = "全查数据",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData select_company(){
        return adminService.select_company();
    }

    @RequestMapping(value ="/update_company",method =RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改系统设置",notes = "修改",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="xi_tong_name",value = "系统名称",dataType = "String",required = true),
            @ApiImplicitParam(name ="website",value = "网址",dataType = "String",required = true),
            @ApiImplicitParam(name ="qi_ye_name",value = "企业名称",dataType = "String",required = true),
            @ApiImplicitParam(name ="qi_ye_abstract",value = "企业简介",dataType = "String",required = true),
            @ApiImplicitParam(name ="phone",value = "联系电话",dataType = "String",required = true),
            @ApiImplicitParam(name ="on_off",value = "验证码开关状态（1开2关）",dataType = "int",required = true),
            @ApiImplicitParam(name ="timeout",value = "登录超时最大时长",dataType = "int",required = true),
            @ApiImplicitParam(name ="wrong_num",value = "最大登录错误次数",dataType = "int",required = true),
            @ApiImplicitParam(name ="logo_pic",value = "Logo图片",dataType = "MultipartFile",required = true),
    })
    public ResponseData update_company(@RequestParam(name = "xi_tong_name")String xiTongName,
                                       @RequestParam(name = "website")String website,
                                       @RequestParam(name = "qi_ye_name")String qiYeName,
                                       @RequestParam(name = "qi_ye_abstract")String qiYeAbstract,
                                       @RequestParam(name = "phone")String phone,
                                       @RequestParam(name = "on_off")int onOff,
                                       @RequestParam(name = "timeout")int timeout,
                                       @RequestParam(name = "wrong_num")int wrongNum,
                                       @RequestParam(name = "logo_pic",required = false) MultipartFile[] logoPic,
                                       HttpServletRequest request) throws IOException, SQLException {
        return adminService.update_company(request,xiTongName,website,qiYeName,qiYeAbstract,phone,onOff,timeout,wrongNum,logoPic);
    }

    @RequestMapping(value = "/select_log",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入数据库管理",notes = "数据库管理界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData select_log(){
        return adminService.select_log();
    }

    @RequestMapping(value = "/insert_log",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增数据库备份信息",notes = "新增",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData insert_log(HttpServletRequest request, HttpServletResponse response){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(new Date());
        Principal userPrincipal = request.getUserPrincipal();
        return adminService.insert_log(userPrincipal,date,"0","insert");
    }

    @RequestMapping(value = "/select_variable",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入配置变量界面",notes = "配置变量界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="currentPage",value = "当前页码",dataType = "int",required = true),
            @ApiImplicitParam(name ="pageSize",value = "每页显示记录数",dataType = "int",required = true),
            @ApiImplicitParam(name ="keyWord",value = "关键字查找",dataType = "String",required = false),
    })
    public ResponseData select_variable(@RequestParam(name = "currentPage")int currentPage,
                                        @RequestParam(name = "pageSize")int pageSize,
                                        @RequestParam(name = "keyWord",required = false)String keyWord){
        return adminService.select_variable(currentPage,pageSize,keyWord);
    }

    @RequestMapping(value = "/into_update_variable",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入修改变量界面",notes = "修改变量界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "变量ID",dataType = "int",required = true)
    })
    public ResponseData into_update_variable(@RequestParam(name = "id")int id){
        return adminService.into_update_variable(id);
    }

    @RequestMapping(value = "/update_variable",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改变量",notes = "修改",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "变量ID",dataType = "int",required = true),
            @ApiImplicitParam(name ="message",value = "变量说明",dataType = "String",required = true),
            @ApiImplicitParam(name ="bian_hao",value = "变量编号",dataType = "int",required = true),
            @ApiImplicitParam(name ="title",value = "变量名",dataType = "String",required = true),
            @ApiImplicitParam(name ="value",value = "变量值",dataType = "String",required = true)
    })
    public ResponseData update_variable(@RequestParam(name = "id")int id,
                                        @RequestParam(name = "message")String message,
                                        @RequestParam(name = "bian_hao")int bianHao,
                                        @RequestParam(name = "title")String title,
                                        @RequestParam(name = "value")String value){
        return adminService.update_variable(id,bianHao,message,title,value);
    }

    @RequestMapping(value = "/select_role",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入角色权限界面",notes = "角色权限界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="currentPage",value = "当前页码",dataType = "int",required = true),
            @ApiImplicitParam(name ="pageSize",value = "每页显示记录数",dataType = "int",required = true),
            @ApiImplicitParam(name ="keyWord",value = "关键字查找",dataType = "String",required = false),
    })
    public ResponseData select_role(@RequestParam(name = "currentPage")int currentPage,
                                    @RequestParam(name = "pageSize")int pageSize,
                                    @RequestParam(name = "keyWord",required = false)String keyWord){
        return adminService.select_role(currentPage,pageSize,keyWord);
    }

    @RequestMapping(value = "/into_add_role",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "进入添加角色界面",notes = "添加角色界面",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData into_add_role(){
        return adminService.into_add_role();
    }

    @RequestMapping(value = "/add_role",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加角色",notes = "添加",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="rolename",value = "角色名称",dataType = "String",required = true),
            @ApiImplicitParam(name ="note",value = "备注",dataType = "String",required = false),
            @ApiImplicitParam(name ="perm",value = "权限值，每个权限中间用|符号隔开",dataType = "String",required = true),
    })
    public ResponseData add_role(@RequestParam(name = "rolename")String rolename,
                                 @RequestParam(name = "note",required = false)String note,
                                 @RequestParam(name = "perm")String perm){
        return adminService.add_role(rolename,note,perm);
    }

    @RequestMapping(value = "/into_update_role",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入修改角色界面",notes = "修改角色界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "变量ID",dataType = "int",required = true)
    })
    public ResponseData into_update_role(@RequestParam(name = "id")int id){
        return adminService.into_update_role(id);
    }

    @RequestMapping(value = "/update_role",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改角色信息",notes = "修改",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "角色ID",dataType = "int",required = true),
            @ApiImplicitParam(name ="rolename",value = "角色名称",dataType = "String",required = true),
            @ApiImplicitParam(name ="note",value = "备注",dataType = "String",required = false),
            @ApiImplicitParam(name ="perm",value = "权限值，每个权限中间用|符号隔开",dataType = "String",required = true),
    })
    public ResponseData update_role(@RequestParam(name = "id")int id,
                                    @RequestParam(name = "rolename")String rolename,
                                    @RequestParam(name = "note",required = false)String note,
                                    @RequestParam(name = "perm")String perm){
        return adminService.update_role(id,rolename,note,perm);
    }

    @RequestMapping(value = "/select_user",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入管理员列表界面",notes = "管理员列表界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="currentPage",value = "当前页码",dataType = "int",required = true),
            @ApiImplicitParam(name ="pageSize",value = "每页显示记录数",dataType = "int",required = true),
            @ApiImplicitParam(name ="keyWord",value = "关键字查找",dataType = "String",required = false),
    })
    public ResponseData select_user(@RequestParam(name = "currentPage")int currentPage,
                                    @RequestParam(name = "pageSize")int pageSize,
                                    @RequestParam(name = "keyWord",required = false)String keyWord){
        return adminService.select_user(currentPage,pageSize,keyWord);
    }

    @RequestMapping(value = "/into_add_user",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入添加管理员列表界面",notes = "添加管理员列表界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData into_add_user(){
        return adminService.into_add_user();
    }

    @RequestMapping(value = "/add_user",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加管理员",notes = "添加",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="number",value = "登陆账号",dataType = "String",required = true),
            @ApiImplicitParam(name ="realname",value = "真实姓名",dataType = "String",required = true),
            @ApiImplicitParam(name ="password",value = "密码",dataType = "String",required = true),
            @ApiImplicitParam(name ="sex",value = "性别",dataType = "String",required = true),
            @ApiImplicitParam(name ="rolename",value = "相关角色名称",dataType = "String",required = true),
            @ApiImplicitParam(name ="phone",value = "手机号",dataType = "String",required = true),
            @ApiImplicitParam(name ="email",value = "邮箱",dataType = "String",required = true),
    })
    public ResponseData add_user(@RequestParam(name = "number")String number,
                                 @RequestParam(name = "realname")String realname,
                                 @RequestParam(name = "password")String password,
                                 @RequestParam(name = "sex")String sex,
                                 @RequestParam(name = "rolename")String rolename,
                                 @RequestParam(name = "phone")String phone,
                                 @RequestParam(name = "email")String email){
            return adminService.add_user(number,realname,password,sex,rolename,phone,email);
    }

    @RequestMapping(value = "/into_update_user",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "进入修改管理员界面",notes = "修改管理员界面",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "变量ID",dataType = "int",required = true)
    })
    public ResponseData into_update_user(@RequestParam(name = "id")int id){
        return adminService.into_update_user(id);
    }

    @RequestMapping(value = "/update_user",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改管理员",notes = "修改",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "管理员ID",dataType = "int",required = true),
            @ApiImplicitParam(name ="number",value = "登陆账号",dataType = "String",required = true),
            @ApiImplicitParam(name ="realname",value = "真实姓名",dataType = "String",required = true),
            @ApiImplicitParam(name ="password",value = "密码",dataType = "String",required = true),
            @ApiImplicitParam(name ="sex",value = "性别",dataType = "String",required = true),
            @ApiImplicitParam(name ="rolename",value = "相关角色名称",dataType = "String",required = true),
            @ApiImplicitParam(name ="phone",value = "手机号",dataType = "String",required = true),
            @ApiImplicitParam(name ="email",value = "邮箱",dataType = "String",required = true),
    })
    public ResponseData update_user(@RequestParam(name = "id")int id,
                                    @RequestParam(name = "number")String number,
                                    @RequestParam(name = "realname")String realname,
                                    @RequestParam(name = "password")String password,
                                    @RequestParam(name = "sex")String sex,
                                    @RequestParam(name = "rolename")String rolename,
                                    @RequestParam(name = "phone")String phone,
                                    @RequestParam(name = "email")String email){
        return adminService.update_user(id,number,realname,password,sex,rolename,phone,email);
    }

    @RequestMapping(value = "/roleNameList",method = RequestMethod.GET)
    @ApiOperation(value = "角色名称下拉框",notes = "角色名称下拉框")
    @ResponseBody
    public ResponseData roleNameList(){
        return adminService.roleNameList();
    }

    @RequestMapping(value = "/allroleMenu",method = RequestMethod.GET)
    @ApiOperation(value = "权限菜单",notes = "权限菜单")
    @ResponseBody
    public ResponseData allroleMenu(){
        return adminService.allroleMenu();
    }

    @RequestMapping(value = "/delete_role",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除角色",notes = "删除角色界面",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误"),
            @ApiResponse(code=507,message="没有这个id")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "ID",dataType = "int",required = true)
    })
    public ResponseData delete_role(@RequestParam(name = "id")int id){
        return adminService.delete_role(id);
    }

    @RequestMapping(value = "/delete_user",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除管理员",notes = "删除管理员界面",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误"),
            @ApiResponse(code=507,message="没有这个id")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id",value = "ID",dataType = "int",required = true)
    })
    public ResponseData delete_user(@RequestParam(name = "id")int id){
        return adminService.delete_user(id);
    }

    @RequestMapping(value = "/bu_men",method = RequestMethod.GET)
    @ApiOperation(value = "寻炸部门",notes = "寻炸部门")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误"),
            @ApiResponse(code=507,message="没有这个id")
    })
    public ResponseData fenPei(){
        return adminService.bu_men();
    }

    @RequestMapping(value = "/fen_pei",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分配部门",notes = "分配部门",httpMethod = "post")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误"),
            @ApiResponse(code=507,message="没有这个id")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name ="number",value = "用户名",dataType = "String",required = true),
            @ApiImplicitParam(name ="name",value = "部门名",dataType = "String",required = true)
    })
    public ResponseData fen_pei(@RequestParam(name = "number")String number,
                                @RequestParam(name = "name")String buMenName){
        return adminService.fen_pei(number,buMenName);
    }
}
